package com.chuangping.util;


import com.chuangping.mvp.model.entity.CommonBean;
import com.example.basiclib.application.ServiceException;
import com.example.basiclib.utils.Utils;
import com.jess.arms.utils.ArmsUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * Created by winry on 2016/8/26.
 */
public class HttpResultFunction<T> implements Function<CommonBean<T>, T> {

    public void exception_event() {
    }

    @Override
    public T apply(@NonNull CommonBean<T> tCommonBean) throws Exception {
        if (tCommonBean.getCode() != 1) {//!1
            exception_event();
            Observable.just(tCommonBean.getMessage())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(message -> ArmsUtils.makeText(Utils.getApp(), message));
            throw  new ServiceException(tCommonBean.getMessage(), tCommonBean.getCode());
        } else {//正常的状态
            return tCommonBean.getData();
        }
    }
}
